/*
 * @Description: 流程管理相关接口
 * @Author: Rfan
 * @Date: 2023-01-11 10:00:25
 * @LastEditTime: 2023-01-11 10:02:13
 */

import { flowRequest } from '@/utils/request';
import type { Key } from 'react';
import type { IPageQuery } from '@/interfaces/global';

// 流程列表

export function fetchFlowList(data: IPageQuery) {
  return flowRequest({
    url: `/model/page`,
    method: 'post',
    data
  });
}
export interface IProcessListResponse {
  id: Key;
  modelType: string;
  name: string;
  icon: null;
  iconColor: null;
  processKey: string;
  content: string;
  creator: string;
  createTime: Date;
  updater: string;
  updateTime: Date;
  remark: string;
}

// 增加流程
export interface IAddFlowItemParams {
  icon?: string;
  iconColor?: string;
  modelType: number;
  name: string;
  remark?: string;
}
export function addFlowItem(data: IAddFlowItemParams) {
  return flowRequest({
    url: `/model/create`,
    method: 'post',
    data
  });
}

// 编辑流程
export interface IEditFlowItemParams extends IAddFlowItemParams {
  id: number;
}
export function editFlowItem(data: IEditFlowItemParams) {
  return flowRequest({
    url: `/model/update`,
    method: 'post',
    data
  });
}

// 删除流程
export function deleteFlowItem(data: Key[]) {
  return flowRequest({
    url: `/model/delete`,
    method: 'post',
    data
  });
}

export interface IProcessTypeResponse {
  id: string;
  name: string;
  pid: string;
  parentName: null | string;
  children?: IProcessTypeResponse[] | null;
  flowType: string;
  sort: number;
}

// 获取流程类型
export function fetchProcessType() {
  return flowRequest({
    url: `/flowtype/list`,
    method: 'get'
  });
}

// 根据ID查询流程图
export function fetchFlowDiagram(id: Key) {
  return flowRequest({
    url: `/model/info/${id}`,
    method: 'get'
  });
}

// 保存流程图
export interface ISaveFlowDiagramParams {
  content: string;
  id: number;
}
export function saveFlowDiagram(data: ISaveFlowDiagramParams) {
  return flowRequest({
    url: `/model/saveModel`,
    method: 'post',
    data
  });
}

// 发布流程
export function deployProcessApi(id: Key) {
  return flowRequest({
    url: `/model/deploy/${id}`,
    method: 'get'
  });
}
